package tvp.ltdd.fake_call_sms.receiver;

import tvp.ltdd.fake_call_sms.R;
import tvp.ltdd.fake_call_sms.helper.MessageHelper;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Vibrator;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class MessageReceiver extends BroadcastReceiver {

	private String name, number, textmessage;
	private int type;
	private NotificationManager myNotificationManager;
	private int notificationIdOne = 222;

	@Override
	public void onReceive(Context context, Intent intent) {
		PowerManager pm = (PowerManager) context
				.getSystemService(Context.POWER_SERVICE);
		PowerManager.WakeLock wl = pm.newWakeLock(
				PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
		wl.acquire();
		Bundle extra = intent.getExtras();
		if (extra != null) {
			name = extra.getString("name");
			number = extra.getString("number");
			textmessage = extra.getString("textmessage");
			type = extra.getInt("type");
		}
		if (type == 1) {
			displayNotification(context);
			Uri notification = RingtoneManager
					.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
			Ringtone r = RingtoneManager.getRingtone(context, notification);
			r.play();
			Vibrator vb = (Vibrator) context
					.getSystemService(Context.VIBRATOR_SERVICE);
			// Vibrate for 500 milliseconds
			vb.vibrate(500);
			wl.release();
		}
		Log.v("222222222222222", "insert message");
		MessageHelper.putMessage(context.getContentResolver(), number,
				textmessage, type);
	}

	public void setMessage(Context context, String name, String number,
			String textmessage, int type, int time) {
		AlarmManager am = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(context, MessageReceiver.class);
		intent.putExtra("name", name);
		intent.putExtra("number", number);
		intent.putExtra("textmessage", textmessage);
		intent.putExtra("type", type);
		PendingIntent pi = PendingIntent.getBroadcast(context, 1, intent, 0);
		am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + time, pi);
	}

	public void displayNotification(Context context) {
		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
				context);
		if (name.trim().length() != 0)
			mBuilder.setContentTitle(name);
		else
			mBuilder.setContentTitle(number);
		mBuilder.setContentText(textmessage);
		mBuilder.setTicker(name + " " + textmessage);
		mBuilder.setSmallIcon(R.drawable.stat_notify_mms);
		mBuilder.setLights(Color.RED, 300, 1000);
		mBuilder.setVibrate(new long[] { 1000, 1000 });
		mBuilder.setDefaults(Notification.FLAG_SHOW_LIGHTS);
		myNotificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		myNotificationManager.notify(notificationIdOne, mBuilder.build());

	}

}
